package handler

import (
	"github.com/gin-gonic/gin"
	"zy14/global"
	"zy14/internal/request"
	"zy14/models"
)

func Register(c *gin.Context) {
	var register request.Register

	if err := c.ShouldBind(&register); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取异常",
			"data": nil,
		})
		return
	}
	user := models.User{
		Username: register.Username,
		Password: register.Password,
		Mobile:   register.Mobile,
	}
	err := global.DB.Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": user.ID,
	})
}
func VideoAdd(c *gin.Context) {
	var videoadd request.Video

	if err := c.ShouldBind(&videoadd); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取异常",
			"data": nil,
		})
		return
	}
	videos := models.Video{
		Title:  videoadd.Title,
		Videos: videoadd.Videos,
	}
	err := global.DB.Create(&videos).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "视频发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "视频发布成功",
		"data": videos.ID,
	})
}
func VideoList(c *gin.Context) {
	var videoList request.VideoList

	if err := c.ShouldBind(&videoList); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取异常",
			"data": nil,
		})
		return
	}
	var videos []*models.Video
	err := global.DB.Find(&videos).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "视频展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "视频展示成功",
		"data": videos,
	})
}
